package com.jxauaia.contest.controller;

import com.jxauaia.contest.common.response.Result;
import com.jxauaia.contest.model.dto.ChangePasswordDTO;
import com.jxauaia.contest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users/account")
public class AccountController {

    @Autowired
    private UserService userService;
    
    /**
     * 修改密码
     */
    @PutMapping("/{userId}/password")
    @PreAuthorize("authentication.principal.id == #userId")
    public Result<Boolean> changePassword(
            @PathVariable Integer userId,
            @RequestBody ChangePasswordDTO changePasswordDTO) {
        boolean result = userService.changePassword(userId, changePasswordDTO);
        return Result.success(result);
    }
    
    /**
     * 注销账号（停用）
     */
    @PutMapping("/{userId}/deactivate")
    @PreAuthorize("authentication.principal.id == #userId")
    public Result<Boolean> deactivateAccount(@PathVariable Integer userId) {
        boolean result = userService.deactivateAccount(userId, userId);
        return Result.success(result);
    }
    
    /**
     * 退出登录（前端实现，清除token）
     */
    @PostMapping("/logout")
    public Result<Boolean> logout() {
        // 前端清除token即可，后端可以记录日志
        return Result.success(true);
    }
}